package org.geektimes.projects.user.repository;


import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.sql.DerbyConnectionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;
import java.util.logging.Logger;

public class UserRepositoryImpl implements UserRepository {

    private static Logger logger = Logger.getLogger(UserRepositoryImpl.class.getName());


    @Override
    public boolean save(User user) {
        Connection connection = DerbyConnectionManager.getConnection();
        PreparedStatement preparedStatement = null;
        int row = 0;
        //编写sql语句
        String sql = "INSERT INTO USERS (NAME,PASSWORD,EMAIL,PHONENUMBER) VALUES(?,?,?,?)";
        try {
            //预编译sql语句
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getName());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getEmail());
            preparedStatement.setString(4, user.getPhoneNumber());
            //执行sql语句
            row = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return row > 0 ? true : false;
    }

    @Override
    public boolean deleteById(Long userId) {
        return false;
    }

    @Override
    public boolean update(User user) {
        return false;
    }

    @Override
    public User getById(Long userId) {
        return null;
    }

    @Override
    public User getByNameAndPassword(String userName, String password) {
        return null;
    }

    @Override
    public Collection<User> getAll() {
        return null;
    }
}
